package com.zdhr.igms.controller;

import com.alibaba.fastjson.JSON;
import com.sun.xml.messaging.saaj.packaging.mime.internet.ContentType;
import com.zdhr.igms.demain.PageBean;
import com.zdhr.igms.demain.RealTimeData;
import com.zdhr.igms.report.service.ReportService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ProjectName: igms
 * @Package: com.zdhr.igms.controller
 * @ClassName: PublicController
 * @Author: luolin
 * @Description: 公共接口
 * @Date: 2019-10-14 14:38
 * @Version: 1.0
 */
@Controller
@RequestMapping("/public")

public class PublicController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Resource
    private ReportService reportService;

    /**
     * 施工管理页面
     * @return
     */
    @RequestMapping(value = "/constructionData")
    public String constructionData(String id, HttpServletRequest request, HttpSession session){
        System.out.println(id);
        RealTimeData realTimeData = reportService.queryRealTimeDataById(id);
        session.setAttribute("realTimeData",realTimeData);
        request.setAttribute("dev_id",id);
        return "igms/constructionData";
    }
    // 参数设置页面
    @RequestMapping(value = "/parammeterSetings")
    public String parammeterSetings(){
        return "igms/parammeterSetings";
    }
    // 暂停页面
    @RequestMapping(value = "/processData")
    public String processData(){
        return "igms/processData";
    }
    // 孔段参数
    @RequestMapping(value = "/holeParamSetings")
    public String holeParamSetings(){
        return "igms/holeParamSetings";
    }
    @RequestMapping(value = "/showData")
    public String showData(){
        List<Map> showDataList = new ArrayList<>();
        return "igms/showData";
    }

    /**
     * 实时数据接收接口
     * @param data
     * @return
     */
    @PostMapping(value = "/realTimeDataAPI")
    @ResponseBody
    public String realTimeDataAPI(String data){
        if(null == data||"{}".equals(data)){
            //无数据
            return "501";
        }
        RealTimeData realTimeData = JSON.parseObject(data,RealTimeData.class);
        if(null != realTimeData){
            try {
                if(null!= realTimeData.getDate_time1()){
                    System.out.println(realTimeData);
                    reportService.insertRealTimeData(realTimeData);
                }
            }catch (Exception e){
                logger.error("实时数据持久化失败！",e);
                return "500";
            }
        }else{
            // 数据错误
            return "502";
        }
        return "200";

    }

    // 查询数据
    @RequestMapping(value = "/queryShowData",produces = {"application/json;charset=utf-8"})
    @ResponseBody
    public PageBean queryShowData(Integer page,Integer limit,String dev_id,Byte online){
        PageBean pageBean = new PageBean(page,limit);
        RealTimeData realTimeData = new RealTimeData(dev_id,online);
        reportService.queryShowData(pageBean,realTimeData);
        pageBean.setCode(0);
        pageBean.setMsg("正在获取数据");
        return pageBean;
    }

    /**
     * 通道切换
     * @param id
     * @param session
     * @return
     */
    @PostMapping(value = "/changeChannel")
    @ResponseBody
    public RealTimeData changeChannel(String id,HttpSession session){
        RealTimeData realTimeData = reportService.queryRealTimeDataById(id);
        session.setAttribute("realTimeData",realTimeData);
        return realTimeData;
    }
    /**
     * 实时更新
     * @param id
     * @param session
     * @return
     */
    @PostMapping(value = "/realTimeChange")
    @ResponseBody
    public RealTimeData realTimeChange(String id,HttpSession session){
        RealTimeData realTimeData = reportService.queryRealTimeDataById(id);
        session.setAttribute("realTimeData",realTimeData);
        return realTimeData;
    }
}
